package com.wxzz.elearing.user.api.app;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.user.dto.*;
import com.wxzz.elearing.user.entity.EnterpriseImprove;
import com.wxzz.elearing.user.service.IEnterpriseImproveService;
import com.wxzz.elearing.user.vo.EnterpriseImproveVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/wx-enterprise-improve")
@Api(tags = "企业改善(小程序)")
public class WxEnterpriseImproveController {

    @Autowired
    private IEnterpriseImproveService enterpriseImproveService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @ApiOperation(value = "发布或修改改善(小程序)", notes = "发布或修改改善(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectStatus", method = RequestMethod.POST)
    public ReturnVO selectStatus(@RequestHeader String token, @RequestBody EnterpriseImprove enterpriseImprove) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        enterpriseImprove.setWxUserId(jsonObject.getLong("id"));
        enterpriseImprove.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        enterpriseImprove.setEnterpriseOrganizationId(jsonObject.getLong("enterpriseOrganizationId"));
        return enterpriseImproveService.saveEnterpriseImprove(enterpriseImprove);
    }

    @ApiOperation(value = "改善列表(小程序)", notes = "改善列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listEnterpriseImprove", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<EnterpriseImprove>>> listEnterpriseImprove(@RequestHeader String token, @RequestBody EnterpriseImproveDto enterpriseImproveDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return enterpriseImproveService.listEnterpriseImprove(0, jsonObject.getLong("enterpriseId"), enterpriseImproveDto);
    }

    @ApiOperation(value = "改善审核(小程序)", notes = "改善审核(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "updateEnterpriseImprove", method = RequestMethod.POST)
    public ReturnVO updateEnterpriseImprove(@RequestHeader String token, @RequestBody EnterpriseImproveUpdateDto enterpriseImproveUpdateDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return enterpriseImproveService.wxUpdateEnterpriseImprove(jsonObject.getLong("id"), enterpriseImproveUpdateDto);
    }

    @ApiOperation(value = "查询改善详情(小程序)", notes = "查询改善详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectEnterpriseImproveDetail/{enterpriseImproveId}", method = RequestMethod.POST)
    public ReturnVO<EnterpriseImprove> selectEnterpriseImproveDetail(@PathVariable Long enterpriseImproveId) {
        return enterpriseImproveService.selectEnterpriseImproveDetail(enterpriseImproveId);
    }

    @ApiOperation(value = "删除改善(小程序)", notes = "删除改善(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "deleteEnterpriseImprove", method = RequestMethod.POST)
    public ReturnVO deleteEnterpriseImprove(@PathVariable Long enterpriseImproveId) {
        return enterpriseImproveService.deleteEnterpriseImprove(enterpriseImproveId);
    }

    @ApiOperation(value = "查询我的改善(小程序)", notes = "查询我的改善(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listMyEnterpriseImprove", method = RequestMethod.POST)
    public ReturnVO listMyEnterpriseImprove(@RequestHeader String token, @RequestBody MyEnterpriseImproveDto myEnterpriseImproveDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return enterpriseImproveService.listMyEnterpriseImprove(jsonObject.getLong("id"), myEnterpriseImproveDto);
    }

    @ApiOperation(value = "我的改善审核(小程序)", notes = "我的改善审核(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listMyCheckEnterpriseImprove", method = RequestMethod.POST)
    public ReturnVO listMyCheckEnterpriseImprove(@RequestHeader String token, @RequestBody MyCheckEnterpriseImproveDto myCheckEnterpriseImproveDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return enterpriseImproveService.listMyCheckEnterpriseImprove(jsonObject.getLong("id"), myCheckEnterpriseImproveDto);
    }

    @ApiOperation(value = "展示当前企业改善之星(小程序)", notes = "展示当前企业改善之星(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectSumByEnterpriseImprove", method = RequestMethod.POST)
    public ReturnVO selectSumByEnterpriseImprove(HttpServletResponse response, @RequestHeader String token, @RequestBody SumByEnterpriseImproveDto sumByEnterpriseImproveDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return enterpriseImproveService.selectSumByEnterpriseImprove(response,jsonObject.getLong("enterpriseId"), sumByEnterpriseImproveDto);
    }

    @ApiOperation(value = "查询当前企业改善统计(小程序)", notes = "查询当前企业改善统计(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectEnterpriseImproveVo", method = RequestMethod.POST)
    public ReturnVO<EnterpriseImproveVo> selectEnterpriseImproveVo(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return enterpriseImproveService.selectEnterpriseImproveVo(jsonObject.getLong("enterpriseId"));
    }


}
